#include "tcpsocket.h"

TcpSocket::TcpSocket()
{
    socketID = "";
    // 绑定读信号
    connect(this, SIGNAL(readyRead()), this, SLOT(socket_slot_readyRead()));
    // 绑定关闭连接信号
    connect(this, SIGNAL(disconnected()), this, SLOT(socket_slot_disconnected()));

    connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(socket_slot_bytesWritten(qint64)));
}

// 设置id
void TcpSocket::setSocketID(QString socketID)
{
    this->socketID = socketID;
}

void TcpSocket::sendMessage(QByteArray message)
{
    this->write(message);
}

void TcpSocket::socket_slot_readyRead()
{
    // 发送信号，通知上层有数据
    emit socket_signal_readMessage(this->socketID);
}

void TcpSocket::socket_slot_disconnected()
{
    // 发送信号，通知上层连接已关闭
    emit socket_signal_disconnected(this->socketID);
}

void TcpSocket::socket_slot_bytesWritten(qint64 bytes)
{
    emit socket_signal_bytesWritten(this->socketID, bytes);
}

TcpSocket::~TcpSocket()
{

}

